<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <treeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <hr>
        <el-tree default-expand-all :data="departs" :props="defaultProps">
          <treeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
      <addDept ref="addDept" :show-dialog.sync="showDialog" :node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import treeTools from './commponents/tree-tool'
import addDept from './commponents/add-dept' // 引入新增部门组件
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
export default {
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '组织架构',
        manager: '负责人',
        id: ''
      },
      showDialog: false, // 显示窗体
      node: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      // this.company = { name: res.companyName, manager: '负责人' }
      this.departs = transListToTreeData(res.depts, '')
    },
    addDepts(treeNode) {
      // console.log('子组件点击新增触发,获取到被点击的那个组件的数据')
      // console.log(treeNode)
      // 1. 控制弹窗
      this.showDialog = true
      // 2. 将获取的 treeNode 传给弹窗
      this.node = treeNode
    },
    async editDepts(treeNode) {
      // 因为是编辑, 除了弹窗以外, 还需要执行子组件获取数据方法
      // 来实现数据回显
      await this.$refs.addDept.getDepartDetail(treeNode.id)
      this.showDialog = true
      this.node = treeNode
    }
  }
}
</script>

<style  scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>>

